package com.grape.action.system;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.grape.action.common.BaseAction;
import com.grape.domain.system.Role;
import com.grape.service.system.RoleService;


@Namespace("/")
@Scope("prototype")
@Controller
@ParentPackage("json-default")
public class RoleAction extends BaseAction<Role>{
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private RoleService rs;
	
	@Action(value="role_list",results=@Result(name="success",type="json"))
	public String list() {
		List<Role> list = rs.findAll();
		ServletActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
	@Action(value="role_save",results=@Result(name="success",type="json"))
	public String save() {
		
		rs.save(model,permissionIds,menuIds);
		
		Map<String,Object> mp = new HashMap<>();
		mp.put("msg", model.getName()+"保存成功");
		ServletActionContext.getContext().getValueStack().push(mp);
		
		return SUCCESS;
	}
	
	@Action(value="role_delete",results=@Result(name="success",type="redirect",location="/pages/system/role.html"))
	public String delete() {
		
		rs.delete(ids);
		return SUCCESS;
	}
	
	
	private String ids;
	private Integer[] permissionIds;
	private String menuIds;

	public void setPermissionIds(Integer[] permissionIds) {
		this.permissionIds = permissionIds;
	}

	public void setMenuIds(String menuIds) {
		this.menuIds = menuIds;
	}
	
	public void setIds(String ids) {
		this.ids = ids;
	}

}